﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Core.DAL;

namespace BAL.eCorp
{
    public class B30BizDocDetails
    {
        #region region: Khai báo hằng
        const string CONST_TABLE_NAME = "B30BizDocDetail";
        #endregion

        #region region: Khai báo biến
        //[Id] [int] IDENTITY(1,1) NOT NULL,
        protected int _Id;
        //[ParentId] [int] NOT NULL,
        protected int _ParentId = -1;
        //[IsGroup] [bit] NOT NULL,
        protected bool _IsGroup = false;
        //[BizDocId] [varchar](16) NOT NULL,
        protected string _BizDocId = string.Empty;
        //[RowId] [varchar](16) NOT NULL,
        protected string _RowId = string.Empty;
        //[ParentRowId] [varchar](16) NULL,
        protected string _ParentRowId = string.Empty;
        //[BuiltinOrder] [smallint] NOT NULL,
        protected int _BuiltinOrder = -1;
        //[DocDate] [smalldatetime] NOT NULL,
        protected DateTime _DocDate;
        //[CustomerCode] [varchar](16) NOT NULL,
        protected string _CustomerCode;
        //[ItemCode] [varchar](24) NOT NULL,
        protected string _ItemCode = string.Empty;
        //[Description] [nvarchar](254) NOT NULL,
        protected string _Description = string.Empty;
        //[Unit] [nvarchar](8) NOT NULL,
        protected string _Unit = string.Empty;
        //[Quantity] [numeric](15,4) NOT NULL,
        protected decimal _Quantity; 
        //[OriginalUnitCost] [numeric](18,2) NOT NULL,
        protected decimal _OriginalUnitCost;
        //[UnitCost] [numeric](18,2) NOT NULL,
        protected decimal _UnitCost;
        //[OriginalAmount] [numeric](18,2) NOT NULL,
        protected decimal _OriginalAmount;
        //[Amount] [numeric](18,2) NOT NULL,
        protected decimal _Amount;

        // Hệ thống
	    protected bool _IsActive = true;
	    protected int _CreatedBy;
	    protected DateTime _CreatedAt;
	    protected int _ModifiedBy;
	    protected DateTime _ModifiedAt;
        #endregion

        #region region: Định nghĩa thuộc tính
        
        //[Id] [int] IDENTITY(1,1) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        //[ParentId] [int] NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public int ParentId
        {
            get { return _ParentId; }
            set { _ParentId = value; }
        }
        //[IsGroup] [bit] NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public bool IsGroup
        {
            get { return _IsGroup; }
            set { _IsGroup = value; }
        }
        //[BizDocId] [varchar](16) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string BizDocId
        {
            get { return _BizDocId; }
            set { _BizDocId = value; }
        }
        //[RowId] [varchar](16) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string RowId
        {
            get { return _RowId; }
            set { _RowId = value; }
        }
        //[ParentRowId] [varchar](16) NULL,
        /// <summary>
        /// 
        /// </summary>
        public string ParentRowId
        {
            get { return _ParentRowId; }
            set { _ParentRowId = value; }
        }
        //[BuiltinOrder] [smallint] NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public int BuiltinOrder
        {
            get { return _BuiltinOrder; }
            set { _BuiltinOrder = value; }
        }
        //[DocDate] [smalldatetime] NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public DateTime DocDate
        {
            get { return _DocDate; }
            set { _DocDate = value; }
        }
        //[CustomerCode] [varchar](16) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string CustomerCode
        {
            get { return _CustomerCode; }
            set { _CustomerCode = value; }
        }
        //[ItemCode] [varchar](24) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string ItemCode
        {
            get { return _ItemCode; }
            set { _ItemCode = value; }
        }
        //[Description] [nvarchar](254) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        //[Unit] [nvarchar](8) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public string Unit
        {
            get { return _Unit; }
            set { _Unit = value; }
        }
        //[Quantity] [numeric](15,4) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public decimal Quantity
        {
            get { return _Quantity; }
            set { _Quantity = value; }
        }
        //[OriginalUnitCost] [numeric](18,2) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public decimal OriginalUnitCost
        {
            get { return _OriginalUnitCost; }
            set { _OriginalUnitCost = value; }
        }
        //[UnitCost] [numeric](18,2) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public decimal UnitCost
        {
            get { return _UnitCost; }
            set { _UnitCost = value; }
        }
        //[OriginalAmount] [numeric](18,2) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public decimal OriginalAmount
        {
            get { return _OriginalAmount; }
            set { _OriginalAmount = value; }
        }
        //[Amount] [numeric](18,2) NOT NULL,
        /// <summary>
        /// 
        /// </summary>
        public decimal Amount
        {
            get { return _Amount; }
            set { _Amount = value; }
        }

        // Hệ thống
        /// <summary>
        /// 
        /// </summary>
        public bool IsActive
        {
            get{return _IsActive;}
            set{_IsActive = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int CreatedBy
        {
            get{return _CreatedBy;}
            set{_CreatedBy = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime CreatedAt
        {
            get{return _CreatedAt;}
            set{_CreatedAt = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int ModifiedBy
        {
            get{return _ModifiedBy;}
            set{_ModifiedBy = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime ModifiedAt
        {
            get{return _ModifiedAt;}
            set{_ModifiedAt = value;}
        }
        #endregion

        #region region: Định nghĩa phương thức
        /// <summary>
        /// 
        /// </summary>
        public B30BizDocDetails()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        public B30BizDocDetails(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                //[Id] [int] IDENTITY(1,1) NOT NULL,
                _Id = reader.GetInt32(reader.GetOrdinal("Id"));
                //[ParentId] [int] NOT NULL,
                _ParentId = reader.GetInt32(reader.GetOrdinal("ParentId"));
                //[IsGroup] [bit] NOT NULL,
                _IsGroup = reader.GetBoolean(reader.GetOrdinal("IsGroup"));
                //[BizDocId] [varchar](16) NOT NULL,
                _BizDocId = reader.GetString(reader.GetOrdinal("BizDocId"));
                //[RowId] [varchar](16) NOT NULL,
                _RowId = reader.GetString(reader.GetOrdinal("RowId"));
                //[ParentRowId] [varchar](16) NULL,
                _ParentRowId = reader.GetString(reader.GetOrdinal("ParentRowId"));
                //[BuiltinOrder] [smallint] NOT NULL,
                _BuiltinOrder = reader.GetInt16(reader.GetOrdinal("BuiltinOrder"));
                //[DocDate] [smalldatetime] NOT NULL,
                if (!reader.IsDBNull(reader.GetOrdinal("DocDate")))
                    _DocDate = reader.GetDateTime(reader.GetOrdinal("DocDate"));
                //[CustomerCode] [varchar](16) NOT NULL,
                _CustomerCode = reader.GetString(reader.GetOrdinal("CustomerCode"));
                //[ItemCode] [varchar](24) NOT NULL,
                _ItemCode = reader.GetString(reader.GetOrdinal("ItemCode"));
                //[Description] [nvarchar](254) NOT NULL,
                _Description = reader.GetString(reader.GetOrdinal("Description"));
                //[Unit] [nvarchar](8) NOT NULL,
                _Unit = reader.GetString(reader.GetOrdinal("Unit"));
                //[Quantity] [numeric](15,4) NOT NULL,
                _Quantity = reader.GetDecimal(reader.GetOrdinal("Quantity"));
                //[OriginalUnitCost] [numeric](18,2) NOT NULL,
                _OriginalUnitCost = reader.GetDecimal(reader.GetOrdinal("OriginalUnitCost"));
                //[UnitCost] [numeric](18,2) NOT NULL,
                _UnitCost = reader.GetDecimal(reader.GetOrdinal("UnitCost"));
                //[OriginalAmount] [numeric](18,2) NOT NULL,
                _OriginalAmount = reader.GetDecimal(reader.GetOrdinal("OriginalAmount"));
                //[Amount] [numeric](18,2) NOT NULL,
                _Amount = reader.GetDecimal(reader.GetOrdinal("Amount"));
                
                // Hệ thống
                _IsActive = reader.GetBoolean(reader.GetOrdinal("IsActive"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy")))
                    _CreatedBy = reader.GetInt32(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedAt")))
                    _CreatedAt = reader.GetDateTime(reader.GetOrdinal("CreatedAt"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModifiedBy")))
                    _ModifiedBy = reader.GetInt32(reader.GetOrdinal("ModifiedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModifiedAt")))
                    _ModifiedAt = reader.GetDateTime(reader.GetOrdinal("ModifiedAt"));
            }
        }
        /// <summary>
        /// Phương thức truy xuất sản phẩm theo ProductID
        /// </summary>
        /// <returns></returns>
        public static B30BizDocDetails GetByID(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            B30BizDocDetails obj = new B30BizDocDetails();

            db.AddParameter("@_Id", o.Id);
            SqlDataReader reader = db.ExecuteReader("sp_B30BizDocDetail_SelectID", CommandType.StoredProcedure);
            if (reader.Read())
            {
                obj = new B30BizDocDetails(reader);
            }

            reader.Close();
            return obj;
        }
        /// <summary>
        /// Phương thức truy xuất toàn bộ dữ liệu bảng sản phẩm
        /// </summary>
        /// <returns></returns>
        public static IList<B30BizDocDetails> GetAll()
        {
            DBHelp db = new DBHelp();
            IList<B30BizDocDetails> list = new List<B30BizDocDetails>();
            SqlDataReader reader = db.ExecuteReader("sp_B30BizDocDetail_SelectAll", CommandType.StoredProcedure);
            while (reader.Read())
            {
                list.Add(new B30BizDocDetails(reader));
            }
            reader.Close();
            return list;
        }
        /// <summary>
        /// Truy xuất sản phẩm theo mã thể loại
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static IList<B30BizDocDetails> GetByBizDocId(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            IList<B30BizDocDetails> list = new List<B30BizDocDetails>();
            db.AddParameter("@_BizDocId", o.BizDocId);
            SqlDataReader reader = db.ExecuteReader("sp_B30BizDocDetail_SelectBizDocId", CommandType.StoredProcedure);
            while (reader.Read())
            {
                list.Add(new B30BizDocDetails(reader));
            }
            reader.Close();
            return list;
        }
        
        /// <summary>
        /// Chèn mới một sản phẩm
        /// </summary>
        /// <param name="o"></param>
        public static void Insert(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            //@_ParentId,
            db.AddParameter("@_ParentId", o.ParentId);
            //@_IsGroup,
            db.AddParameter("@_IsGroup", o.IsGroup);
            //@_BizDocId,
            db.AddParameter("@_BizDocId", o.BizDocId);
            //@_RowId,
            db.AddParameter("@_RowId", o.RowId);
            //@_ParentRowId,
            db.AddParameter("@_ParentRowId", o.ParentRowId);
            //@_BuiltinOrder,
            db.AddParameter("@_BuiltinOrder", o.BuiltinOrder);
            //@_DocDate,
            db.AddParameter("@_DocDate", o.DocDate);
            //@_CustomerCode,
            db.AddParameter("@_CustomerCode", o.CustomerCode);
            //@_ItemCode,
            db.AddParameter("@_ItemCode", o.ItemCode);
            //@_Description,
            db.AddParameter("@_Description", o.Description);
            //@_Unit,
            db.AddParameter("@_Unit", o.Unit);
            //@_Quantity,
            db.AddParameter("@_Quantity", o.Quantity);
            //@_OriginalUnitCost,
            db.AddParameter("@_OriginalUnitCost", o.OriginalUnitCost);
            //@_UnitCost,
            db.AddParameter("@_UnitCost", o.UnitCost);
            //@_OriginalAmount,
            db.AddParameter("@_OriginalAmount", o.OriginalAmount);
            //@_Amount,
            db.AddParameter("@_Amount", o.Amount);
            //@_IsActive,
            db.AddParameter("@_IsActive", o.IsActive);
            //@_CreatedBy,
            db.AddParameter("@_CreatedBy", o.CreatedBy);
            //@_CreatedAt
            db.AddParameter("@_CreatedAt", o.CreatedAt);
            //@_CreatedBy,
            db.AddParameter("@_CreatedBy", o.CreatedBy);
            //@_CreatedAt
            db.AddParameter("@_CreatedAt", DateTime.Now.ToShortDateString());

            db.ExecuteNonQuery("sp_B30BizDocDetail_Insert", CommandType.StoredProcedure);
        }
        /// <summary>
        /// Cập nhật sản phẩm
        /// </summary>
        /// <param name="o"></param>
        public static void Update(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            //@_Id [int],
            db.AddParameter("@_Id", o.Id);
            //@_ParentId [int],
            db.AddParameter("@_ParentId", o.ParentId);
            //@_IsGroup [bit],
            db.AddParameter("@_IsGroup", o.IsGroup);
            //@_BizDocId [varchar](16),
            db.AddParameter("@_BizDocId", o.BizDocId);
            //@_RowId [varchar](16),
            db.AddParameter("@_RowId", o.RowId);
            //@_ParentRowId [varchar](16),
            db.AddParameter("@_ParentRowId", o.ParentRowId);
            //@_BuiltinOrder [smallint],
            db.AddParameter("@_BuiltinOrder", o.BuiltinOrder);
            //@_DocDate [smalldatetime],
            db.AddParameter("@_DocDate", o.DocDate);
            //@_CustomerCode [varchar](16),
            db.AddParameter("@_CustomerCode", o.CustomerCode);
            //@_ItemCode [varchar](24),
            db.AddParameter("@_ItemCode", o.ItemCode);
            //@_Description [nvarchar](254),
            db.AddParameter("@_Description", o._Description);
            //@_Unit [nvarchar](8),
            db.AddParameter("@_Unit", o.Unit);
            //@_Quantity [numeric](15,4),
            db.AddParameter("@_Quantity", o.Quantity);
            //@_OriginalUnitCost [numeric](18,2),
            db.AddParameter("@_OriginalUnitCost", o.OriginalUnitCost);
            //@_UnitCost [numeric](18,2),
            db.AddParameter("@_UnitCost", o.UnitCost);
            //@_OriginalAmount [numeric](18,2),
            db.AddParameter("@_OriginalAmount", o.OriginalAmount);
            //@_Amount [numeric](18,2),
            db.AddParameter("@_Amount", o.Amount);
            //@_IsActive [bit],
            db.AddParameter("@_IsActive", o.IsActive);
            //@_ModifiedBy [int],
            db.AddParameter("@_ModifiedBy", o.ModifiedBy);
            //@_ModifiedAt [datetime]
            db.AddParameter("@_ModifiedAt", o.ModifiedAt);

            db.ExecuteNonQuery("sp_B30BizDocDetail_Update", CommandType.StoredProcedure);
        }
        /// <summary>
        /// Xóa sản phẩm theo thể loại
        /// </summary>
        /// <param name="o"></param>
        public static void Delete(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            db.AddParameter("@_Id", o.Id);
            db.ExecuteNonQuery("sp_B30BizDocDetail_Delete", CommandType.StoredProcedure);
        }
        /// <summary>
        /// Xóa sản phẩm theo thể loại
        /// </summary>
        /// <param name="o"></param>
        public static void DeleteByBizDocId(B30BizDocDetails o)
        {
            DBHelp db = new DBHelp();
            db.AddParameter("@_Id", o.Id);
            db.ExecuteNonQuery("sp_B30BizDocDetail_DeleteBizDocId", CommandType.StoredProcedure);
        }
        #endregion
    }
}